﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Cryomancer.StarChart;

namespace ScratchPad
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class Window1 : Window
	{
		private Point startDragPoint = new Point();
		private Point startLinePoint = new Point();

		public Window1()
		{
			InitializeComponent();
		}

		private void viewport3D_MouseWheel(object sender, MouseWheelEventArgs e)
		{
			Point3D oldPos  = this.cameraMain.Position;
			double  newZRaw = oldPos.Z - (e.Delta * 0.05d);
			Point3D newPos  = new Point3D(oldPos.X, oldPos.Y, newZRaw < 1 ? 1 : newZRaw);
			
			this.cameraMain.Position = newPos;
		}

		private void viewport3D_MouseMove(object sender, MouseEventArgs e)
		{
			if (e.RightButton == MouseButtonState.Pressed)
			{
				if (this.startDragPoint.X == 0 && this.startDragPoint.Y == 0)
				{
					this.startDragPoint = e.GetPosition(this.viewport3D);
					return;
				}

				Point[] points = new Point[2];
				points[0] = this.startDragPoint;
				points[1] = e.GetPosition(this.viewport3D);

				double halfWidth  = this.viewport3D.ActualWidth  / 2.0d;
				double halfHeight = this.viewport3D.ActualHeight / 2.0d;

				for (int i = 0; i < 2; i++)
				{
					points[i].X -= halfWidth;
					points[i].Y -= halfHeight;
					points[i].Y *= -1;
				}

				PolarCoordinate pol1 = new PolarCoordinate(points[0]);
				PolarCoordinate pol2 = new PolarCoordinate(points[1]);

				double deltaTheta = pol2.Theta - pol1.Theta;

				if (deltaTheta == 0.0d)
				{
					return;
				}

				Vector3D oldUpDir = this.cameraMain.UpDirection;

				PolarCoordinate polarUpDir = new PolarCoordinate(new Point(oldUpDir.X, oldUpDir.Y));
				polarUpDir.Theta -= deltaTheta;

				Point newUpPoint = polarUpDir.ToPoint();

				Vector3D newUpDir = new Vector3D(newUpPoint.X, newUpPoint.Y, 0);
				newUpDir.Normalize();

				this.cameraMain.UpDirection = newUpDir;

				this.startDragPoint = e.GetPosition(this.viewport3D);
			}
			
		}

		private void ScrollViewer_MouseMove(object sender, MouseEventArgs e)
		{
			/*
			Point p = Mouse.GetPosition(this.scrollViewerMain);

			this.labelRaw.Content = String.Format("[{0},{1}]", p.X, p.Y);

			double halfWidth  = this.scrollViewerMain.ActualWidth  / 2.0d;
			double halfHeight = this.scrollViewerMain.ActualHeight / 2.0d;

			p.X -= halfWidth;
			p.Y -= halfHeight;
			p.Y *= -1;

			this.labelCentered.Content = String.Format("[{0},{1}]", p.X, p.Y);
			 * */
		}

		private void viewport3D_MouseDown(object sender, MouseButtonEventArgs e)
		{
			if (e.ChangedButton == MouseButton.Right)
			{
				this.startDragPoint = e.GetPosition(this.viewport3D);
			}
		}

		private void viewport3D_MouseUp(object sender, MouseButtonEventArgs e)
		{
			if (e.ChangedButton == MouseButton.Right)
			{
				this.startDragPoint = new Point(0,0);
			}
		}
	}
}
